package leetcode.top100;

import leetcode.util.CommonUtils;
import leetcode.剑指offer.pojo.ListNode;
import org.junit.Test;

import java.util.List;

/**
 * @author ：zsy
 * @date ：Created 2022/11/10 9:48 PM
 * @description：
 */
public class _021_合并两个有序链表 {
    @Test
    public void test() {
        ListNode a = CommonUtils.transferArrToListNode(new int[]{1, 2, 4});
        ListNode b = CommonUtils.transferArrToListNode(new int[]{1, 3, 4});
        CommonUtils.printListNode(new Solution().mergeTwoLists(a, b));
    }

    class Solution {
        public ListNode mergeTwoLists(ListNode A, ListNode B) {
            ListNode head = new ListNode(-1);
            ListNode tmp = head;
            ListNode a = A;
            ListNode b = B;
            while (a != null || b != null) {
                tmp.next = (a == null ? Integer.MAX_VALUE : a.val) > (b == null ? Integer.MAX_VALUE : b.val) ? b : a;
                if (a != null && tmp.next.val == a.val) {
                    a = a.next;
                } else {
                    b = b.next;
                }
                tmp = tmp.next;
            }
            return head.next;
        }
    }
}
